﻿app.controller('LoginCtrl', function ($scope, $state, $modal, $log) {
    //定义open方法
    $scope.items = ['item1', 'item2', 'item3'];
    $scope.open = function (windowClass, templateUrl, size) {
        var modalInstance = $modal.open({
            windowClass: windowClass,
            templateUrl: templateUrl,
            controller: 'ModalInstanceCtrl',
            size: size,
            resolve: {
                items: function () {
                    return $scope.items;
                }
            }
        });
        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };
    $scope.username = '';
    $scope.password = '';
    //登录判断方法
    $scope.onLoginButtonClick = function () {
        if ($scope.username == '' || $scope.username == null) {
            angular.element('#userName').focus();
            return;
        }
        if ($scope.password == '' || $scope.password == null) {
            angular.element('#passWord').focus();
            return;
        }
        var username = $scope.username;
        var password = $scope.password;

        if (username == "admin" && password == "111111") {
            //调用ModalDemoCtrl中的弹框
            $scope.open('modal-message modal-success', 'SuccessModal.html');
            console.log("登录成功");
            $state.go('app.dashboard');
        }else {
            $scope.open('modal-message modal-warning', 'WarningModal.html');
            console.log("失败了。。。。")
        }
        // $http({
        //     method: "POST",
        //     url: "../../data/login.json",
        //     data: {
        //         username: username,
        //         password: password
        //     }
        // }).then(function (data) {
        //     console.log(data);
        // });
    };
    //失去焦点时执行登录函数
    $scope.todoSomething=function(e){
        $scope.onLoginButtonClick();
    }
});
app.controller('ModalInstanceCtrl', function ($scope, $modalInstance, items) {
    $scope.items = items;
    $scope.selected = {
        item: $scope.items[0]
    };
    $scope.ok = function () {
        $modalInstance.close($scope.selected.item);
    };
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
});